<?php

/**
 * 获取需要审核的爆料任务
 */
if($uri == "/api/admin/details"){
    $page = isset($_GET['page']) ? $_GET['page'] : 1;
    $size = isset($_GET['size']) ? $_GET['size'] : 50;
    $details = $cnSql->read("submit_details", $page, $size, "status=0",$total);
    $outJson->data = $details;
    $outJson->total = $total;
    $outJson->msg = "获取成功";
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

//提交审核结果
if($uri == "/api/admin/audit"){
    $post = json_decode(file_get_contents('php://input'), true);
    $id = isset($post['id']) ? $post['id'] : 0;
    //检查是否已经审核过
    $detail = $cnSql->read_only("submit_details", "id = $id");
    if(!$detail){
        $outJson->msg = "记录不存在";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }
    if($detail['status'] != 0){
        $outJson->msg = "该记录已审核,不能重复审核";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }
    $status = isset($post['status']) ? $post['status'] : 0;
    if($status == 1){
        $user_id = $detail['user_id'];
        $template = $cnSql->read_only("templates", "id = $detail[template_id]");
        $cnSql->custom("UPDATE releases SET status = 3 WHERE id = $detail[task_id]");
        $cnSql->custom("UPDATE users SET balance = balance + $template[price] WHERE id = $user_id");
        $cnSql->update_json("submit_details", array("status" => $status,"price" => $template[price]), "id = $id");

    }else{
        $cnSql->custom("UPDATE releases SET status = 4 WHERE id = $detail[task_id]");
        $cnSql->update_json("submit_details", array("status" => $status,"price" => 0), "id = $id");
    }
    
    $outJson->msg = "审核成功";
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 获取任务原始数据
 */
if($uri == "/api/admin/details/release"){
    $task_id = isset($_GET['task_id']) ? intval($_GET['task_id']) : 0;
    if(!$task_id) {
        $outJson->msg = "任务ID不能为空";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    $release = $cnSql->read_only("releases", "id = $task_id");
    if(!$release) {
        $outJson->msg = "任务不存在";
        $outJson->code = 404;
        echo json_encode($outJson);
        exit;
    }

    $outJson->data = $release;
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 获取审核统计数据
 */
if($uri == "/api/admin/details/stats") {
    $template_id = isset($_GET['template_id']) ? intval($_GET['template_id']) : null;
    
    $where = "1=1";
    if($template_id) {
        $where .= " AND template_id = $template_id";
    }
    
    $stats = $cnSql->custom("SELECT 
        COUNT(*) as total,
        SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as pending,
        SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as approved,
        SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as rejected
    FROM submit_details WHERE $where")[0];
    
    $outJson->data = array(
        'total' => intval($stats['total']),
        'pending' => intval($stats['pending']),
        'approved' => intval($stats['approved']),
        'rejected' => intval($stats['rejected'])
    );
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}
?>